#ifndef __CPPTUTORAUTOPTR_H__
#define __CPPTUTORAUTOPTR_H__

#include <iostream>

using namespace std;

template <class T>
class CcppTutorAutoPtr
{
	private:
        T *aptr;
	public:
        explicit CcppTutorAutoPtr (T *P = NULL) : aptr (P)
        {
        }

        ~CcppTutorAutoPtr ()
        {
            delete aptr;
        }
        	
        T& operator  * ()
        {
            return *aptr;
        }
        T* operator  -> ()
        {
            return aptr;
        }        
    	
        //Copy constructor
        CcppTutorAutoPtr (CcppTutorAutoPtr<T> &source)
        {
    		aptr = source.aptr;
    		//Since the ownership gets transferred lets point the source to 0
            source.aptr = 0;
        }
        
        CcppTutorAutoPtr& operator=(CcppTutorAutoPtr<T> &source)
        {
            //check for self reference
            if(aptr != source.aptr)
            {
                //flush the existing memory
                delete aptr;
                aptr = 0;

                // use the new pointer
                aptr = source.aptr;

                //Since the ownership gets transferred lets point the source to 0
                source.aptr = 0;
                return *this;
            }
            else
	    {
                // SAU
		// check for return valiue here
		return *this;
            }
        }                

    	//Get the value of pointer
        T* get()
        {
        	return aptr;
    	}

    	//reset the value of pointer
    	void reset(T *newVal)
    	{
    		delete aptr;
    		aptr = newVal;
    	}

    	//release the pointer and returns the ownership
    	T* release()
    	{
        	T *t = aptr;
    		aptr = 0;
    		return t;
    	}
};

#endif //__CPPTUTORAUTOPTR_H__

